package jst.householdmanager.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import jst.householdmanager.database.table.Shops_has_Items;

public class Shops_has_ItemsDataSource {

	public static final String TABLE_NAME = "Shops_has_Items";
	public static final String COLUMN_SHOP_ID = "shopItem_shop_ID";
	public static final String COLUMN_ITEM_ID = "shopItem_item_ID";

	public static final String[] ALL_COLUMNS = { COLUMN_SHOP_ID, COLUMN_ITEM_ID };

	public static final int[] COLUMN_DATATYPE = { 0, 0 };

	public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ( " + COLUMN_SHOP_ID + " INTEGER, " + COLUMN_ITEM_ID
			+ " INTEGER,  PRIMARY KEY ( " + COLUMN_SHOP_ID + " , " + COLUMN_ITEM_ID + ") );";

	public static void save(Shops_has_Items dataRecord) {
		ContentValues values = new ContentValues();
		values.put(COLUMN_SHOP_ID, dataRecord.getShop_ID());
		values.put(COLUMN_ITEM_ID, dataRecord.getItem_ID());
		SQLiteDataSource.getDatabase().insert(TABLE_NAME, null, values);
	}

	public static void delete(String selection, String[] selectionArgs) {
		SQLiteDataSource.getDatabase().delete(TABLE_NAME, selection, selectionArgs);
	}

	public static Shops_has_Items read(String selection, String[] selectionArgs) {
		Shops_has_Items dataRecord = null;
		Cursor cursor = SQLiteDataSource.getDatabase().query(TABLE_NAME, ALL_COLUMNS, selection, selectionArgs, null, null, null);
		cursor.moveToFirst();
		if (!cursor.isAfterLast()) {
			dataRecord = cursorTo(cursor);
		}
		cursor.close();

		return dataRecord;
	}

	public static List<Shops_has_Items> readAll(String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
		List<Shops_has_Items> dataRecords = new ArrayList<Shops_has_Items>();
		Cursor cursor = SQLiteDataSource.getDatabase().query(TABLE_NAME, ALL_COLUMNS, selection, selectionArgs, groupBy, having, orderBy);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			dataRecords.add(cursorTo(cursor));
			cursor.moveToNext();
		}
		cursor.close();

		return dataRecords;
	}

	public static void delete(Shops_has_Items dataRecord) {
		delete(COLUMN_SHOP_ID + " = ? and " + COLUMN_ITEM_ID + " = ?",
				new String[] { Integer.toString(dataRecord.getShop_ID()), Integer.toString(dataRecord.getItem_ID()) });
	}

	public static void delete(String field, int idValue) {
		delete(field + " = ?", new String[] { Integer.toString(idValue) });
	}

	public static Shops_has_Items read(int shopId, int itemId) {
		return read(COLUMN_SHOP_ID + " = ? and " + COLUMN_ITEM_ID + " = ?", new String[] { Integer.toString(shopId), Integer.toString(itemId) });

	}

	public static List<Shops_has_Items> readAll(String selection, String[] selectionArgs) {
		return readAll(selection, selectionArgs, null, null, null);
	}

	public static List<Shops_has_Items> readAll() {
		return readAll(null, null, null, null, null);
	}

	public static List<Shops_has_Items> readAll(String field, int idValue) {
		return readAll(field + " = ?", new String[] { Integer.toString(idValue) }, null, null, null);
	}

	private static Shops_has_Items cursorTo(Cursor cursor) {
		return new Shops_has_Items(cursor.getInt(0), cursor.getInt(1));
	}

}
